package ar.com.photo_admin.service.security

import grails.plugins.springsecurity.SpringSecurityService;

import org.springframework.security.authentication.AuthenticationProvider
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication;

class FacebookAutenticationProvider implements AuthenticationProvider {
  
  SpringSecurityService springSecurityService
  PhotoAdminUserDetailsService userDetailsService

  Authentication authenticate(Authentication authentication) {
	  
	  def user = userDetailsService.loadUserByFacebookUid(authentication.principal)

	  if (user) {
		  def token = new UsernamePasswordAuthenticationToken(user, user.password, user.authorities)
		  token.details = authentication.details
		  return token
	  } else
		  return null
  }

  boolean supports(Class authentication) {
	  return FacebookAuthenticationToken.class.isAssignableFrom(authentication)
  }
}